﻿Public Class StackPanel : Inherits UIPanel

	Public Horizontal As Boolean

	Public Overrides Sub Resize(Rectangle As System.Drawing.Rectangle)
		MyBase.Resize(Rectangle)
		Dim R = ContentRectangle
		Dim x = R.Left
		Dim y = R.Top
		Dim temp As Integer
		If Horizontal Then
			temp = (From c In Children Where c.Visible Select c.Height).Max
		Else
			temp = (From c In Children Where c.Visible Select c.Width).Max
		End If
		For Each Child In Children
			If Child.Visible Then
				If Horizontal Then
					Child.Resize(New Rectangle(x, R.Top, Child.Width, temp))
					x += Child.Width
				Else
					Child.Resize(New Rectangle(R.Left, y, temp, Child.Height))
					y += Child.Height
				End If
			End If
		Next

	End Sub

	Public Overrides ReadOnly Property Height As Integer
		Get
			If Horizontal Then
				Return MaxHeight() + MyBase.Height
			Else
				Return SumHeight() + MyBase.Height
			End If
			Return MyBase.Height
		End Get
	End Property

	Function MaxHeight() As Integer
		Return (From c In Children Where c.Visible Select c.Height).Max
	End Function

	Function SumHeight() As Integer
		Return (From c In Children Where c.Visible Select c.Height).Sum
	End Function

	Public Overrides ReadOnly Property Width As Integer
		Get
			If Horizontal Then
				Return SumWidth() + MyBase.Width
			Else
				Return MaxWidth() + MyBase.Width
			End If
			Return MyBase.Height
		End Get
	End Property

	Function MaxWidth() As Integer
		Return (From c In Children Where c.Visible Select c.Width).Max
	End Function

	Function SumWidth() As Integer
		Return (From c In Children Where c.Visible Select c.Width).Sum
	End Function

End Class
